﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;

namespace Red.Controls.Shell
{
    public partial class PageControl : UserControl
    {
        public PageControl()
        {
            InitializeComponent();
        }
    }

    /// <summary>
    ///     Drawer's element.
    /// </summary>
    [ContentProperty("Content")]
    public class Page : ElementFacade<PageControl>
    {
        #region Data

        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register( "Title", typeof( string ), typeof( Page ),
                new FrameworkPropertyMetadata( null ) );

        public new static readonly DependencyProperty ContentProperty =
            DependencyProperty.Register( "Content", typeof( object ), typeof( Page ),
                new FrameworkPropertyMetadata( null, ContentPropertyChanged ), IsContentValid );

        public static readonly DependencyProperty IsListedProperty =
            DependencyProperty.Register( "IsListed", typeof( bool ), typeof( Page ),
                new FrameworkPropertyMetadata( true, OnIsListedPropertyChanged ) );

        #endregion Data

        #region Properties

        /// <summary>
        ///     Text that is displayed on the page tab
        /// </summary>
        public string Title
        {
            get { return (string)GetValue( TitleProperty ); }
            set { SetValue( TitleProperty, value ); }
        }

        /// <summary>
        ///     An object being a page content.
        /// </summary>
        public new object Content
        {
            get { return (object)GetValue( ContentProperty ); }
            set { SetValue( ContentProperty, value ); }
        }

        /// <summary>
        ///     Gets/Sets if the page is listed as a tab in a drawer.
        ///     (Notice, that changing Visibility won't remove the page's tab)
        /// </summary>
        public bool IsListed
        {
            get { return (bool)GetValue( IsListedProperty ); }
            set { SetValue( IsListedProperty, value ); }
        }

        #endregion

        #region Events

        public event EventHandler IsListedChanged;

        #endregion Events

        #region Event handlers

        private static void ContentPropertyChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
        {
            ( d as Page ).Element.Content = e.NewValue;
        }

        private static void OnIsListedPropertyChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
        {
            Page page = (Page)d;
            page.IsListedChanged?.Invoke( page, new EventArgs() );
        }

        #endregion

        #region Private methods

        private static bool IsContentValid( object value )
        {
            return !( value is Drawer || value is Frame || value is Page ); // prevent framework-ception
        }

        #endregion Private methods
    }
}
